﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;

namespace YArchitech.MEP.Electric.CableLay
{
	public class CableTraySchemaDataManager
	{
		public static void SetData(Element element, List<CableTraySchemaData> datas)
		{
			if (element == null || datas == null || datas.Count <= 0)
			{
				return;
			}
			Guid guid = new Guid("472E2919-4AB2-4D12-B51F-99D17682E154");
			Schema schema = Schema.Lookup(guid);
			if (schema == null)
			{
				SchemaBuilder schemaBuilder = new SchemaBuilder(guid);
				schemaBuilder.SetSchemaName("HY_CableTray_ESData");
				schemaBuilder.AddArrayField("Cable_Number", typeof(string));
				schemaBuilder.AddArrayField("Cable_Model", typeof(string));
				schemaBuilder.AddArrayField("Cable_Spec", typeof(string));
				schemaBuilder.AddArrayField("Cable_Section", typeof(double)).SetUnitType(0);
				schemaBuilder.AddArrayField("Remark", typeof(string));
				schema = schemaBuilder.Finish();
			}
			Entity entity = new Entity(schema);
			List<string> list = new List<string>();
			List<string> list2 = new List<string>();
			List<string> list3 = new List<string>();
			List<double> list4 = new List<double>();
			List<string> list5 = new List<string>();
			foreach (CableTraySchemaData cableTraySchemaData in datas)
			{
				list.Add(cableTraySchemaData.CableNumber);
				list2.Add(cableTraySchemaData.Model);
				list3.Add(cableTraySchemaData.Spec);
				list4.Add(cableTraySchemaData.Section);
				list5.Add(string.Empty);
			}
			Field field = schema.GetField("Cable_Number");
			entity.Set<IList<string>>(field, list);
			Field field2 = schema.GetField("Cable_Model");
			entity.Set<IList<string>>(field2, list2);
			Field field3 = schema.GetField("Cable_Spec");
			entity.Set<IList<string>>(field3, list3);
			Field field4 = schema.GetField("Cable_Section");
			entity.Set<IList<double>>(field4, list4, 0);
			Field field5 = schema.GetField("Remark");
			entity.Set<IList<string>>(field5, list5);
			element.SetEntity(entity);
		}

		public static bool GetData(Element element, ref List<CableTraySchemaData> datas)
		{
			if (element == null)
			{
				return false;
			}
			Schema schema = Schema.Lookup(new Guid("472E2919-4AB2-4D12-B51F-99D17682E154"));
			if (schema == null)
			{
				return false;
			}
			Entity entity = element.GetEntity(schema);
			if (entity == null || entity.Schema == null)
			{
				return false;
			}
			Field field = schema.GetField("Cable_Number");
			IList<string> list = entity.Get<IList<string>>(field);
			Field field2 = schema.GetField("Cable_Model");
			IList<string> list2 = entity.Get<IList<string>>(field2);
			Field field3 = schema.GetField("Cable_Spec");
			IList<string> list3 = entity.Get<IList<string>>(field3);
			Field field4 = schema.GetField("Cable_Section");
			IList<double> list4 = entity.Get<IList<double>>(field4, 0);
			Field field5 = schema.GetField("Remark");
			entity.Get<IList<string>>(field5);
			int num = 0;
			while (num < list.Count<string>() && num < list2.Count<string>() && num < list3.Count<string>() && num < list4.Count<double>())
			{
				CableTraySchemaData cableTraySchemaData = new CableTraySchemaData();
				cableTraySchemaData.CableNumber = list[num];
				cableTraySchemaData.Model = list2[num];
				cableTraySchemaData.Spec = list3[num];
				cableTraySchemaData.Section = list4[num];
				datas.Add(cableTraySchemaData);
				num++;
			}
			return true;
		}

		public static bool HasValidSchemaData(Element element)
		{
			if (element == null)
			{
				return false;
			}
			Schema schema = Schema.Lookup(new Guid("472E2919-4AB2-4D12-B51F-99D17682E154"));
			if (schema == null)
			{
				return false;
			}
			Entity entity = element.GetEntity(schema);
			return entity != null && entity.Schema != null;
		}

		public static void DeleteSchemaData(Element element)
		{
			if (element == null)
			{
				return;
			}
			Schema schema = Schema.Lookup(new Guid("472E2919-4AB2-4D12-B51F-99D17682E154"));
			if (schema == null)
			{
				return;
			}
			element.DeleteEntity(schema);
		}

		private const string m_GUID = "472E2919-4AB2-4D12-B51F-99D17682E154";

		private const string m_SchemaName = "HY_CableTray_ESData";

		private const string m_CableNumber = "Cable_Number";

		private const string m_CableModel = "Cable_Model";

		private const string m_CableSpec = "Cable_Spec";

		private const string m_CableSection = "Cable_Section";

		private const string m_Remark = "Remark";
	}
}
